package com.br.sae.controls;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.br.sae.conexaobd.SistemaDAO;
import com.br.sae.model.dao.DisciplinaBD;
import com.br.sae.model.entitys.Disciplina;
import com.br.sae.model.entitys.Usuario;

public class LogicaDisciplinaCrud implements Logica{

	@Override
	public String executa(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		
		String metodo = req.getParameter("Metodo");
		System.out.println(metodo);
		

		if(metodo.equals("inserirDisciplina")){
			String nome = req.getParameter("nome");
			Disciplina disciplina = new Disciplina(nome);
			inserirDisciplina(disciplina);
			return "/mvc?logica=LogicaFormDisciplinas";
			
		}else if(metodo.equals("alterarDisciplina")){
			String nome = req.getParameter("nome");
			Disciplina disciplina = new Disciplina(nome);
			disciplina.setId(Integer.valueOf(req.getParameter("id")));
			alterarDisciplina(disciplina);
			return "/mvc?logica=LogicaFormDisciplinas";
		}else{			
			excluirDisciplina(Integer.valueOf(req.getParameter("id")));
			return "/mvc?logica=LogicaFormDisciplinas";
		}
	}

	private void inserirDisciplina(Disciplina disciplina){
		DisciplinaBD disciplinaBD = new DisciplinaBD();
		disciplinaBD.incluirDisciplina(disciplina);
	}

	private void alterarDisciplina(Disciplina disciplina){
		DisciplinaBD disciplinaBD = new DisciplinaBD();
		disciplinaBD.alterarDisciplina(disciplina);
	}

	private void excluirDisciplina(int id){
		DisciplinaBD disciplinaBD = new DisciplinaBD();
		SistemaDAO sis = new SistemaDAO();
		Disciplina disciplina = (Disciplina) sis.find(Disciplina.class, id);
		disciplinaBD.removerDisciplina(disciplina);
	}

}
